//
//  UIImage+XYX.m
//  E-Cycle(XYXMain)
//
//  Created by 象萌cc003a on 17/2/20.
//  Copyright © 2017年 xmcc003a. All rights reserved.
//

#import "UIImage+Color.h"

@implementation UIImage (Color)
+(UIImage *)loadWithName:(NSString *)name{
    CGFloat scale = [[UIScreen mainScreen]scale];
    NSString *path;
    if (scale == 3.0) {
        name = [name stringByAppendingString:@"@3x"];
    }else{
        name = [name stringByAppendingString:@"@2x"];
    }
    path = [[NSBundle mainBundle]pathForResource:name ofType:@"png"];
    if (path == nil) {
        return nil;
    }else{
        NSError *error = nil;
        NSData *data = [NSData dataWithContentsOfFile:path options:NSDataReadingUncached error:&error];
        if (error==nil) {
            UIImage *image = [UIImage imageWithData:data];
            return image;
        }
        return nil;
    }
}

+(UIImage *)imageWithColor:(UIColor *)color size:(CGSize)size corner:(CGFloat)radius{
    UIGraphicsBeginImageContext(size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGMutablePathRef path = CGPathCreateMutable();
    CGAffineTransform transform = CGAffineTransformIdentity;
    CGPathMoveToPoint(path, &transform, 0, 0);
    CGPathAddRoundedRect(path, &transform, CGRectMake(0, 0, size.width, size.height), radius, radius);
    CGContextAddPath(context, path);
    CGContextSetFillColorWithColor(context, [color CGColor]);
    CGContextFillPath(context);
    CGPathRelease(path);
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;
}

- (UIImage*)transformWidth:(CGFloat)width
                    height:(CGFloat)height {
    
    CGFloat destW = width;
    CGFloat destH = height;
    CGFloat sourceW = width;
    CGFloat sourceH = height;
    
    CGImageRef imageRef = self.CGImage;
    CGContextRef bitmap = CGBitmapContextCreate(NULL,
                                                destW,
                                                destH,
                                                CGImageGetBitsPerComponent(imageRef),
                                                4*destW,
                                                CGImageGetColorSpace(imageRef),
                                                (kCGBitmapByteOrder32Little | kCGImageAlphaPremultipliedFirst));
    
    CGContextDrawImage(bitmap, CGRectMake(0, 0, sourceW, sourceH), imageRef);
    
    CGImageRef ref = CGBitmapContextCreateImage(bitmap);
    UIImage *resultImage = [UIImage imageWithCGImage:ref];
    CGContextRelease(bitmap);
    CGImageRelease(ref);
    return resultImage;
}

+(UIImage *)imageWithColor:(UIColor *)color size:(CGSize)size{
    UIGraphicsBeginImageContext(size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, [color CGColor]);
    CGContextFillRect(context, CGRectMake(0, 0, size.width, size.height));
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;
}

+ (UIImage *)imageWithColor:(UIColor *)color {
    CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, [color CGColor]);
    CGContextFillRect(context, rect);
    UIImage *theImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return theImage;
}

@end
